﻿using CommonType;
using MediatR;
using S_Gridding.Domains.BasicData;
using S_Gridding.Infrastructure.Interface;
using S_Gridding.Read.Api.Command.AssemType;
using SqlSugar;

namespace S_Gridding.Read.Api.CommandHands.AssemType
{
    /// <summary>
    /// 分页 显示部件类型表
    /// </summary>
    public class ShowAssemTypeCommandHander : IRequestHandler<ShowAssemTypeCommand, PageResule<AssemTypeInfo>>
    {
        private readonly IDapperAssemTypeRepository _dapperAssemTypeRepository;  //注入 dapper 部件类型表

        public ShowAssemTypeCommandHander(IDapperAssemTypeRepository dapperAssemTypeRepository)
        {
            _dapperAssemTypeRepository = dapperAssemTypeRepository;
        }
        public async Task<PageResule<AssemTypeInfo>> Handle(ShowAssemTypeCommand request, CancellationToken cancellationToken)
        {
            var query = $"select * from AssemTypeInfo where 1=1 and IsDel!=TRUE";

            var totalCount = await _dapperAssemTypeRepository.SelectAsync(request.PageSize, query);

            query += $" order by AssemTypeId desc limit {(request.PageIndex - 1) * request.PageSize},@PageSize";

            var List = await _dapperAssemTypeRepository.SelectAsync(request.PageSize, query);

            return new PageResule<AssemTypeInfo>
            {
                TotalCount = totalCount.Count(),
                PageCount = (int)Math.Ceiling((List.Count() * 1.0) / request.PageSize),
                Data = List
            };
        }
    }
}
